package org.bees.cms.web.tag;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.jsp.JspWriter;

import net.sf.json.JSONArray;

import org.apache.velocity.Template;
import org.apache.velocity.app.Velocity;
import org.bees.cms.web.util.velocity.Context;
import org.bees.cms.web.util.velocity.element.Button;

public class Operation extends Tag {
	private static final long serialVersionUID = -6425138129713288259L;

	private String create;
	private String modify;
	private String remove;
	private String screen;
	private String key;
	private String buttons;
	private List<Button> attributes = new ArrayList<Button>();

	@Override
	protected void before() throws Exception {
		JSONArray array = JSONArray.fromObject(buttons == null || buttons.trim().equals("") ? "[]" : buttons);
		for (int i = 0; i < array.size(); i++) {
			Button button = new Button();
			button.from(array.getJSONObject(i));
			attributes.add(button);
		}
	}

	@Override
	protected void analyze(JspWriter writer) throws Exception {
		Template template = Velocity.getTemplate("operation.vm.html");
		Context context = context();
		context.put("create", create);
		context.put("modify", modify);
		context.put("remove", remove);
		context.put("screen", screen);
		context.put("key", key);
		context.put("buttons", attributes);
		template.merge(context, writer);
	}

	@Override
	protected void after() throws Exception {
		create = null;
		modify = null;
		remove = null;
		buttons = null;
		attributes.clear();
	}

	public String getCreate() {
		return create;
	}

	public void setCreate(String create) {
		this.create = create;
	}

	public String getModify() {
		return modify;
	}

	public void setModify(String modify) {
		this.modify = modify;
	}

	public String getRemove() {
		return remove;
	}

	public void setRemove(String remove) {
		this.remove = remove;
	}

	public String getScreen() {
		return screen;
	}

	public void setScreen(String screen) {
		this.screen = screen;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public String getButtons() {
		return buttons;
	}

	public void setButtons(String buttons) {
		this.buttons = buttons;
	}

	public List<Button> getAttributes() {
		return attributes;
	}

	public void setAttributes(List<Button> attributes) {
		this.attributes = attributes;
	}

}
